import request from '../utils/request'
import appConfig from '../config'

const area = '/AppPay'

export const create = (data) => {
  return request({
    url: `${area}/WXPayCreate2`,
    method: 'post',
    data
  })
}

export const wxPay = (params) => {
  const { timeStamp, nonceStr, paySign } = params;
  return new Promise((resolve, reject) => {
    try {
      WeixinJSBridge.invoke("getBrandWCPayRequest", {
        timeStamp, //时间戳，自1970年以来的秒数
        nonceStr, //随机串
        paySign, //微信签名
        package: params.package,
        appId: appConfig.appId, //公众号名称，由商户传入
        signType: "MD5", //微信签名方式：
      }, (res) => {
        resolve(res);
      });
    } catch (error) {
      reject({
        ok: 0,
        msg: '请在微信客户端中进行支付'
      });
      throw new Error('请在微信客户端中进行支付')
    }
  })
}
